{{> warning}}
{{> error}}

<ol class="breadcrumb">
  <li><a class="link" href="/">Streams</a>
  <li class="active">{{name}}</li>
</ol>

<div class="row">
    <div class="col-md-6">
        <blockquote>
            <p>Topic: <strong>{{name}}</strong></p>
            {{#if is_multi_ordered}}
                <a class="label label-primary">Order</a>
            {{/if}}
            {{#if is_ext}}
                <a class="label label-primary">Ext</a>
            {{/if}}
            {{#if is_channel_auto_create_disabled}}
                <a class="label label-primary">CACD</a>
            {{/if}}
        </blockquote>
    </div>
</div>

{{#unless nodes.length}}
<div class="row">
    <div class="col-md-6">
        <h4>Topic Message Queue</h4>
        <div class="alert alert-warning">
            <h4>Notice</h4> No producers exist for this topic.
            <p>See <a href="/lookup">Lookup</a> for more information.
        </div>
    </div>
</div>
{{else}}
<div class="row topic-actions">
    <div class="col-md-2">
        <button class="btn btn-medium btn-danger" data-action="delete" {{#if login}}{{else}}disabled{{/if}}>Delete Topic</button>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
    <div class="toggle">
        <h4>Topic Coordinator Status
            <span>
                <a> >>></a>
            </span>
        </h4>
    </div>
    <div class="canHide" style="display: none;">
    <table class="table table-bordered table-condensed">
        <tr>
            <th>Partition ID</th>
            {{#if dcnsqlookupd}}
                <th>DC</th>
            {{/if}}
            <th>ISR and Catchup Info</th>
            <th>Sync Status</th>
        </tr>
        {{#each nodes}}
        <tr>
            {{#if topic_partition}}
            <th style="text-align:center;vertical-align:middle" rowspan="{{syncing_num}}">{{topic_partition}}</th>
            {{else}}
            <th>N/A</th>
            {{/if}}
            {{#if ../dcnsqlookupd}}
                <td>{{dc}}</td>
            {{/if}}
            {{#if isr_stats.length}}
                <td>{{isr_stats.[0].hostname}}(ID:{{isr_stats.[0].node_id}})</td>
                <td>Synced</td>
            {{else}}
                <td>ISR Empty!!</td>
                <td>Unknown</td>
            {{/if}}
        </tr>
            {{#each isr_stats}}
            {{#if @first}}
            {{else}}
        <tr>
            {{#if ../../../dcnsqlookupd}}
            <td>{{../../../dc}}</td>
            {{/if}}
            <td>{{hostname}}(ID:{{node_id}})</td>
            <td>Synced</td>
        </tr>
        {{/if}}
        {{/each}}
        {{#each catchup_stats}}
        <tr>
            <td>{{hostname}}(ID:{{node_id}})</td>
            <td>Catching up: {{progress}}%</td>
        </tr>
        {{/each}}
        {{/each}}
    </table>
    </div>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
    <div class="toggle">
        <h4>Topic Message Queue
        <span>
            <a> >>></a>
        </span>
        </h4>
    </div>
    <div class="canHide" style="display: none;">
    <table class="table table-bordered table-condensed">
        {{#if e2e_processing_latency.percentiles.length}}
        <tr>
            <th colspan="{{#if graph_active}}6{{else}}5{{/if}}"></th>
            <th></th>
            <th colspan="{{e2e_processing_latency.percentiles.length}}">E2E Processing Latency</th>
        </tr>
        {{/if}}
        <tr>
            {{#if dcnsqlookupd.length}}
            <th>DC</th>
            {{/if}}
            <th>NSQd Host</th>
            <th>PartitionInfo</th>
            <th>QueueStart ~ QueueSize</th>
            <th>Messages</th>
            {{#if graph_active}}<th>Rate</th>{{/if}}
            <th>Channels</th>
            <th>
                <p>Hourly Pub Size</p>
            </th>
            {{#each e2e_processing_latency.percentiles}}
                <th>{{floatToPercent quantile}}<sup>{{percSuffix quantile}}</sup></th>
            {{/each}}
        </tr>
        {{#each nodes}}
        <tr>
            {{#if ../../dcnsqlookupd.length}}
            <td>
                {{dc}}
            </td>
            {{/if}}
            <td>
                <button class="btn-link red tombstone-link" data-node="{{node}}" data-topic="{{../name}}" style="padding: 0 6px; border: 0;">✘</button>
                {{#if show_broadcast_address}}
                {{hostname_port}} (<a class="link" href="/nodes/{{node}}">{{node}}</a>)
                {{else}}
                <a class="link" href="/nodes/{{node}}">{{hostname_port}}</a>
                {{/if}}
                {{#if paused}} <span class="label label-primary">paused</span>{{/if}}
            </td>
            <td>{{commafy topic_partition}}</td>
            <td>{{commafy backend_start}} ~ {{commafy backend_depth}}</td>
            <td>{{commafy message_count}}</td>
            {{#if ../graph_active}}
                <td class="bold rate" target="{{rate "topic" node statsd_name ""}}"></td>
            {{/if}}
            <td>{{commafy ../channels.length}}</td>
            <td>
                <a href="#" data-toggle="tooltip" title={{partition_hourly_pubsize}}>
                    {{commafy hourly_pubsize}}
                </a>
            </td>
            {{#if e2e_processing_latency.percentiles.length}}
                {{#each e2e_processing_latency.percentiles}}
                    <td><span title="{{floatToPercent quantile}}: min = {{nanotohuman min}}, max = {{nanotohuman max}}">{{nanotohuman average}}</span></td>
                {{/each}}
            {{/if}}
        </tr>
        {{#if ../graph_active}}
            <tr class="graph-row">
                <td></td>
                <td></td>
                <td></td>
                <td><a href="{{large_graph "topic" node statsd_name "" "message_count"}}"><img width="120" src="{{sparkline "topic" node statsd_name "" "message_count"}}"></a></td>
                <td></td>
                <td></td>
                <td></td>
                {{#if e2e_processing_latency.percentiles.length}}
                <td colspan="{{e2e_processing_latency.percentiles.length}}">
                    <a href="{{large_graph "e2e" node e2e_processing_latency "" "e2e_processing_latency"}}"><img width="120" height="20"  src="{{sparkline "e2e" node e2e_processing_latency "" "e2e_processing_latency"}}"></a>
                </td>
                {{/if}}
            </tr>
        {{/if}}
        {{/each}}
        <tr class="info">
            <td>Total:</td>
            {{#if dcnsqlookupd.length}}
            <td></td>
            {{/if}}
            <td></td>
            <td>{{commafy backend_start}} ~ {{commafy backend_depth}}</td>
            <td>{{commafy message_count}}</td>
            {{#if graph_active}}<td class="bold rate" target="{{rate "topic" "*" statsd_name ""}}"></td>{{/if}}
            <td>{{commafy channels.length}}</td>
            <td>
                <a href="#" data-toggle="tooltip" title={{total_partition_hourly_pubsize}}>
                    {{total_hourly_pubsize}}
                </a>
            </td>
            {{#if e2e_processing_latency.percentiles.length}}
                {{#each e2e_processing_latency.percentiles}}
                <td>
                    <span title="{{floatToPercent quantile}}: min = {{nanotohuman min}}, max = {{nanotohuman max}}">{{nanotohuman average}}</span>
                </td>
                {{/each}}
            {{/if}}
        </tr>
        {{#if graph_active}}
            <tr class="graph-row">
                <td></td>
                <td></td>
                <td></td>
                <td><a href="{{large_graph "topic" "*" statsd_name "" "message_count"}}"><img width="120" src="{{sparkline "topic" "*" statsd_name "" "message_count"}}"></a></td>
                <td></td>
                <td></td>
                <td></td>
                {{#if e2e_processing_latency.percentiles.length}}
                <td colspan="{{e2e_processing_latency.percentiles.length}}">
                    <a href="{{large_graph "e2e" "*" e2e_processing_latency "" "e2e_processing_latency"}}"><img width="120" height="20"  src="{{sparkline "e2e" node e2e_processing_latency "" "e2e_processing_latency"}}"></a>
                </td>
                {{/if}}
            </tr>
        {{/if}}
    </table>
    </div>
    </div>
</div>
{{/unless}}

<div class="row">
    <div class="col-md-12">
        <div class="toggle">
            <h4>Message Size Distribution
            <span>
                <a> >>></a>
            </span>
            </h4>
        </div>
        <div class="canHide" style="display: none;">
        <table class="table table-bordered table-condensed">
            <tr>
                <th>NSQd Host</th>
                <th>< 100byte</th>
                <th>< 1KB</th>
                <th>< 2KB</th>
                <th>< 4KB </th>
                <th>< 8KB</th>
                <th>< 16KB</th>
                <th>< 32KB</th>
                <th>< 64KB</th>
                <th>< 128KB</th>
                <th>< 256KB</th>
                <th>< 512KB</th>
                <th>< 1MB</th>
                <th>< 2MB</th>
                <th>< 4MB</th>
                <th>Not Used</th>
                <th>Not Used</th>
            </tr>
            {{#each nodes}}
            <tr>
                <td>
                    <button class="btn-link red tombstone-link" data-node="{{node}}" data-topic="{{../name}}" style="padding: 0 6px; border: 0;">✘</button>
                    {{#if show_broadcast_address}}
                        {{hostname_port}} (<a class="link" href="/nodes/{{node}}">{{node}}</a>)
                    {{else}}
                        <a class="link" href="/nodes/{{node}}">{{hostname_port}}</a>
                    {{/if}}
                    {{#if paused}} <span class="label label-primary">paused</span>{{/if}}
                </td>
                {{#each msg_size_stats}}
                    <td>{{this}}</td>
                {{/each}}
            </tr>
            {{/each}}
        </table>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <div class="toggle">
            <h4>Message Latency Distribution
                <span>
                    <a> >>></a>
                </span>
            </h4>
        </div>
        <div class="canHide" style="display: none;">
            <table class="table table-bordered table-condensed">
                <tr>
                    <th>NSQd Host</th>
                    <th>< 1024us</th>
                    <th>< 2ms</th>
                    <th>< 4ms</th>
                    <th>< 8ms</th>
                    <th>< 16ms</th>
                    <th>< 32ms</th>
                    <th>< 64ms</th>
                    <th>< 128ms</th>
                    <th>< 256ms</th>
                    <th>< 512ms</th>
                    <th>< 1024ms</th>
                    <th>< 2048ms</th>
                    <th>< 4s</th>
                    <th>< 8s</th>
                    <th>Not Used</th>
                    <th>Not Used</th>
                </tr>
                {{#each nodes}}
                <tr>
                    <td>
                        <button class="btn-link red tombstone-link" data-node="{{node}}" data-topic="{{../name}}" style="padding: 0 6px; border: 0;">✘</button>
                        {{#if show_broadcast_address}}
                            {{hostname_port}} (<a class="link" href="/nodes/{{node}}">{{node}}</a>)
                        {{else}}
                            <a class="link" href="/nodes/{{node}}">{{hostname_port}}</a>
                        {{/if}}
                        {{#if paused}} <span class="label label-primary">paused</span>{{/if}}
                    </td>
                    {{#each msg_write_latency_stats}}
                    <td>{{this}}</td>
                    {{/each}}
                </tr>
                {{/each}}
            </table>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <div class="toggle">
            <h4>Topic Producer Connections
                <span>
                    <a> >>></a>
                </span>
            </h4>
        </div>
        <div class="canHide" style="display: none;">
        {{#unless has_client_pub}}
            <div class="alert alert-warning"><h4>Notice</h4>No producer connected to this topic</div>
        {{else}}
            <table id="producerConnect" class="table tablesorter table-bordered table-condensed">
                <thead>
                <tr>
                    <th>Producer Host</th>
                    <th>User-Agent</th>
                    <th>NSQd host</th>
                    <th>Protocol</th>
                    <th>Pub Count</th>
                    <th>Error Count</th>
                    <th>Last Pub Timestamp</th>
                </tr>
                </thead>
                <tbody>
                {{#each nodes}}
                    {{#each client_pub_stats}}
                    <tr>
                        <td title="{{remote_address}}">{{remote_address}}</td>
                        <td>{{#if user_agent.length}}<small>{{user_agent}}</small>{{/if}}</td>
                        <td>{{../node}}</td>
                        <td>{{protocol}}</td>
                        <td>{{pub_count}}</td>
                        <td>{{err_count}}</td>
                        <td>{{last_pub_ts}}</td>
                    </tr>
                    {{/each}}
                {{/each}}
                </tbody>
            </table>
        {{/unless}}
        </div>
    </div>
</div>

<script type="text/javascript">
    (function(){
        $(document).ready(
            function(){
                $("#producerConnect").tablesorter();
            }
        );
    }());
</script>

<div class="row">
    {{#unless channels.length}}
    <div class="col-md-6">
        <h4>Channel Message Queues</h4>
        <div class="alert alert-warning">
            <h4>Notice</h4> No channels exist for this topic.
            <p>Messages will queue at the topic until a channel is created.
        </div>
    {{else}}
    <div class="col-md-12">
        <h4>Channel Message Queues</h4>
        <table class="table table-bordered table-condensed">
            {{#if e2e_processing_latency.percentiles.length}}
            <tr>
                <th colspan="{{#if graph_active}}10{{else}}9{{/if}}"></th>
                <th colspan="{{e2e_processing_latency.percentiles.length}}">E2E Processing Latency</th>
            </tr>
            {{/if}}
            <tr>
                <th>Channel</th>
                <th>Depth</th>
                <th>DepthTimestamp</th>
                <th>Memory + DiskSize</th>
                <th>In-Flight</th>
                <th>Deferred</th>
                <th>Requeued</th>
                <th>Timed Out</th>
                <th>Messages</th>
                <th>Connections</th>
                {{#each e2e_processing_latency.percentiles}}
                <th>{{floatToPercent quantile}}<sup>{{percSuffix quantile}}</sup></th>
                {{/each}}
            </tr>

            {{#each channels}}
            <tr>
                <th>
                    <a class="link" href="/topics/{{urlencode topic_name}}/{{urlencode channel_name}}">{{channel_name}}</a>
                    {{#if paused}}<span class="label label-primary">paused</span>{{/if}}
                    {{#if skipped}}<span class="label label-primary">skipped</span>{{/if}}
                </th>
                <td>{{commafy depth}}</td>
                <td>{{depth_ts}}</td>
                <td>{{commafy memory_depth}} + {{commafy depth_size}}</td>
                <td>{{commafy in_flight_count}}</td>
                <td>{{commafy deferred_count}}</td>
                <td>{{commafy requeue_count}}</td>
                <td>{{commafy timeout_count}}</td>
                <td>{{commafy message_count}}</td>
                <td>{{commafy clients.length}}</td>
                {{#if e2e_processing_latency.percentiles.length}}
                    {{#each e2e_processing_latency.percentiles}}
                    <td>
                        <span title="{{floatToPercent quantile}}: min = {{nanotohuman min}}, max = {{nanotohuman max}}">{{nanotohuman average}}</span>
                    </td>
                  {{/each}}
                {{/if}}
            </tr>
            {{#if ../graph_active}}
            <tr class="graph-row">
                <td></td>
                <td><a href="{{large_graph "channel" node statsd_name channel_name "depth"}}"><img width="120" height="20"  src="{{sparkline "channel" node statsd_name channel_name "depth"}}"></a></td>
                <td></td>
                <td></td>
                <td><a href="{{large_graph "channel" node statsd_name channel_name "in_flight_count"}}"><img width="120" height="20"  src="{{sparkline "channel" node statsd_name channel_name "in_flight_count"}}"></a></td>
                <td><a href="{{large_graph "channel" node statsd_name channel_name "deferred_count"}}"><img width="120" height="20"  src="{{sparkline "channel" node statsd_name channel_name "deferred_count"}}"></a></td>
                <td><a href="{{large_graph "channel" node statsd_name channel_name "requeue_count"}}"><img width="120" height="20"  src="{{sparkline "channel" node statsd_name channel_name "requeue_count"}}"></a></td>
                <td><a href="{{large_graph "channel" node statsd_name channel_name "timeout_count"}}"><img width="120" height="20"  src="{{sparkline "channel" node statsd_name channel_name "timeout_count"}}"></a></td>
                <td><a href="{{large_graph "channel" node statsd_name channel_name "message_count"}}"><img width="120" height="20"  src="{{sparkline "channel" node statsd_name channel_name "message_count"}}"></a></td>
                <td><a href="{{large_graph "channel" node statsd_name channel_name "clients"}}"><img width="120" height="20"  src="{{sparkline "channel" node statsd_name channel_name "clients"}}"></a></td>
                {{#if e2e_processing_latency.percentiles.length}}
                    <td colspan="{{e2e_processing_latency.percentiles.length}}">
                        <a href="{{large_graph "e2e" node e2e_processing_latency "" "e2e_processing_latency"}}"><img width="120" height="20"  src="{{sparkline "e2e" node e2e_processing_latency "" "e2e_processing_latency"}}"></a>
                    </td>
                {{/if}}
            </tr>
            {{/if}}
            {{/each}}
        </table>
        {{/unless}}
    </div>
</div>

<div class="channel-action">
    <form class="hierarchy">
        <legend>Create Channel</legend>
        <div class="alert alert-info">
            <p>This provides a way to setup a stream hierarchy
            before services are deployed to production.</p>
        </div>
        <div class="form-group">
            <input type="hidden" name="topic" value={{name}}>
            <input type="text" name="channel" placeholder="Channel Name" {{#if login}}{{else}}disabled{{/if}}>
        </div>
        <button class="btn btn-default" type="submit" {{#if login}}{{else}}disabled{{/if}}>Create</button>
    </form>
</div>
